﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
namespace ToaThuoc_PAK.Handler
{
    /// <summary>
    /// Summary description for XML_DonThuocMau
    /// </summary>
    public class XML_DonThuocMau : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        private DAL_DonThuoc dal_DonThuoc;
        public void ProcessRequest(HttpContext context)
        {
            string lenh = Factory.emptyNull(context.Request.QueryString["lenh"]);
            string songay = Factory.emptyNull(context.Request.QueryString["songay"]);
            string dulieu = Factory.emptyNull(context.Request.QueryString["dulieu"]);
            string mabs = Factory.emptyNull(context.Request.QueryString["mabs"]);
            string doituongbn = Factory.emptyNull(context.Request.QueryString["doituong"]);
            string sMaBN = "";
            if (context.Request.QueryString["mabn"] != null)
                sMaBN = Convert.ToString(context.Request.QueryString["mabn"]).Trim();
            //if (sMaBN != "") doituongBN(sMaBN);
            string xmlReturn = "<Root>";
            dal_DonThuoc = new DAL_DonThuoc();
            switch (lenh)
            {
                case "select": xmlReturn += ChonPhacDo(dulieu, sMaBN, doituongbn); break;
                //case "insert": LuuMoiPhacDo(); break;
                //case "update": CapNhatPhacDo(); break;
                //case "delete": XoaPhacDo(dulieu); break;
                //case "danhsach": DanhSachPhacDo_NapLaiDSPD(mabs); break;
                //case "csdspd": DanhSachPhacDo_CSDSPD(mabs); break;
                //case "csqldspd": DSPD_CSQuanLyDSPD(mabs); break;
                //case "tinhsn": tinhsn(songay); break;
            }
            xmlReturn += "</Root>";
            context.Response.ContentType = "text/xml";
            context.Response.Write(xmlReturn);
        }

        private string ChonPhacDo(string dulieu, string MaNV, string DoiTuongBN)
        {
            //        ct.mapd,ct.id, ct.tenthuoc,ct.soluong,ct.donvi,ct.cachdung,ct.landung,ct.luongdung,ct.donvidung,ct.ghichu,ct.thuocid,null loai,null soluongthang,
            //isnull(b.soluongcon,0) as soluongcon,b.idhotrilieu,b.hoachatchinh thanhphan,b.dongia giathamkhao,b.dbname
            string strReturn = "";
            DataTable dt = dal_DonThuoc.LoadDanhSachPhacDo(DoiTuongBN, dulieu, MaNV);
            foreach (DataRow row in dt.Rows)
            {
                strReturn += "<Thuoc     ID=\"" + Factory.emptyNull(row["thuocid"]).TrimEnd(' ') + "\"" + "\n" +
                        "                TenThuoc=\"" + Factory.emptyNull(row["tenthuoc"]).TrimEnd(' ') + "\"" + "\n" +
                        "                DonVi=\"" + Factory.emptyNull(row["DonVi"]) + "\"" + "\n" +
                        "                SoLuong=\"" + Factory.emptyNull(row["SoLuong"]) + "\"" + "\n" +
                        "                CachDung=\"" + Factory.emptyNull(row["CachDung"]) + "\"" + "\n" +
                        "                SoLanDung=\"" + Factory.emptyNull(row["LanDung"]) + "\"" + "\n" +
                        "                LuongDung=\"" + Factory.emptyNull(row["LuongDung"]) + "\"" + "\n" +
                        "                DonViDung=\"" + Factory.emptyNull(row["DonViDung"]) + "\"" + "\n" +
                        "                GhiChu=\"" + Factory.emptyNull(row["GhiChu"]) + "\"" + "\n" +
                        "                SoLuongCon=\"" + Factory.emptyNull(row["SoLuongCon"]) + "\"" + "\n" +
                        "                DonGia=\"" + Factory.emptyNull(row["giathamkhao"]) + "\"" + "\n" +
                        "                DBName=\"" + Factory.emptyNull(row["dbname"]) + "\"" + "\n" +
                        "                ></Thuoc>" + "\n"; ;
            }
            return strReturn;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}